package com.acme.iolab.filesend;

import java.nio.ByteBuffer;

public class IntToByteArrayConverter {

    public static byte[] convert(int value) {
        return new byte[]{
                (byte) ((value >> 24) & 0xFF),
                (byte) ((value >> 16) & 0xFF),
                (byte) ((value >> 8) & 0xFF),
                (byte) (value & 0xFF)};
    }

    public static int convertBack(byte[] bytes) {
        return bytes[3] & 0xFF |
                (bytes[2] & 0xFF) << 8 |
                (bytes[1] & 0xFF) << 16 |
                (bytes[0] & 0xFF) << 24;
    }

    public static int convertBack(ByteBuffer byteBuffer) {
        return byteBuffer.get(3) & 0xFF |
                (byteBuffer.get(2) & 0xFF) << 8 |
                (byteBuffer.get(1) & 0xFF) << 16 |
                (byteBuffer.get(0) & 0xFF) << 24;
    }

}
